package me.goldze.mvvmhabit.http.download;

import android.util.Log;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import okhttp3.ResponseBody;

public abstract class ProgressCallBack<T> {
    private String destFileDir;
    private String destFileName;
    private Disposable mSubscription;

    public abstract void onError(Throwable th);

    public abstract void onSuccess(T t);

    public abstract void progress(long j, long j2);

    public ProgressCallBack(String destFileDir2, String destFileName2) {
        this.destFileDir = destFileDir2;
        this.destFileName = destFileName2;
        subscribeLoadProgress();
    }

    public void onStart() {
    }

    public void onCompleted() {
    }

    public void saveFile(ResponseBody body) {
        InputStream is = null;
        byte[] buf = new byte[2048];
        FileOutputStream fos = null;
        try {
            InputStream is2 = body.byteStream();
            File dir = new File(this.destFileDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            FileOutputStream fos2 = new FileOutputStream(new File(dir, this.destFileName));
            while (true) {
                int len = is2.read(buf);
                if (len == -1) {
                    break;
                }
                fos2.write(buf, 0, len);
            }
            fos2.flush();
            unsubscribe();
            if (is2 != null) {
                try {
                    is2.close();
                } catch (IOException e) {
                    Log.e("saveFile", e.getMessage());
                    return;
                }
            }
            fos2.close();
        } catch (FileNotFoundException e2) {
            e2.printStackTrace();
            if (0 != 0) {
                is.close();
            }
            if (0 != 0) {
                fos.close();
            }
        } catch (IOException e3) {
            e3.printStackTrace();
            if (0 != 0) {
                is.close();
            }
            if (0 != 0) {
                fos.close();
            }
        } catch (Throwable th) {
            if (0 != 0) {
                try {
                    is.close();
                } catch (IOException e4) {
                    Log.e("saveFile", e4.getMessage());
                    throw th;
                }
            }
            if (0 != 0) {
                fos.close();
            }
            throw th;
        }
    }

    public void subscribeLoadProgress() {
        this.mSubscription = RxBus.getDefault().toObservable(DownLoadStateBean.class).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<DownLoadStateBean>() {
            /* class me.goldze.mvvmhabit.http.download.ProgressCallBack.AnonymousClass1 */

            public void accept(DownLoadStateBean progressLoadBean) throws Exception {
                ProgressCallBack.this.progress(progressLoadBean.getBytesLoaded(), progressLoadBean.getTotal());
            }
        });
        RxSubscriptions.add(this.mSubscription);
    }

    public void unsubscribe() {
        RxSubscriptions.remove(this.mSubscription);
    }
}
